package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.BatchListAdapter;
import com.luck.easyfoodbatch.mvp.mine.GetBatchesModel;
import com.luck.easyfoodbatch.mvp.mine.GetBatchesPresenter;
import com.luck.easyfoodbatch.mvp.mine.GetBatchesView;
import com.luck.easyfoodbatch.mvp.model.Batches;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BatchListActivity extends BaseActivity implements GetBatchesView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    private BatchListAdapter adapter;
    private List<List<Batches.DataBean>> list = new ArrayList<>();
    private String gid = "";
    private GetBatchesModel batchesModel;
    private int num = 0;
    private String p_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_batch, "商品批次", "添加批次", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        p_id = (String) SharedPreferencesUtil.getData(this, HttpUtil.P_ID, "3");
        gid = getIntent().getStringExtra("gid");
        num = getIntent().getIntExtra("num", 0);
        batchesModel = new GetBatchesPresenter(this, mContext);
        showProgressDialog(getString(R.string.app_loading));
        batchesModel.getBatches(gid, p_id, 0);
        LinearLayoutManager gridManager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 6F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(gridManager);
        adapter = new BatchListAdapter(mContext, list, onUpdateBatch);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 编辑批次
     */
    public BatchListAdapter.OnUpdateBatch onUpdateBatch = new BatchListAdapter.OnUpdateBatch() {
        @Override
        public void onBatch(List<Batches.DataBean> beanList) {
            Intent intent = new Intent();
            intent.putExtra("list", (Serializable) beanList);
            intent.putExtra("num", num);
            intent.putExtra("gid", gid);
            intent.setClass(mContext, UpdateBatchActivity.class);
            startActivityForResult(intent, 200);
        }
    };

    @Override
    public void rightClick() {
        super.rightClick();
        Intent intent = new Intent();
        intent.putExtra("type", 1);
        intent.putExtra("gid", gid);
        intent.putExtra("num", num);
        intent.setClass(mContext, UpdateBatchActivity.class);
        startActivityForResult(intent, 200);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    showProgressDialog(getString(R.string.app_loading));
                    batchesModel.getBatches(gid, p_id,0);
                    break;
            }
        }
    }

    @Override
    public void batchesSuccess(Batches model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                list = model.getData();
                if (list == null) {
                    list = new ArrayList<>();
                }
                if (list.size() == 0) {
                    iv_nodata.setVisibility(View.VISIBLE);
                } else {
                    iv_nodata.setVisibility(View.GONE);
                }
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }

}
